package com.eostoken.eos_wallet.ui.fragment;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;

import androidx.appcompat.widget.AppCompatImageView;
import androidx.appcompat.widget.AppCompatTextView;
import androidx.constraintlayout.widget.ConstraintLayout;

import com.blankj.utilcode.util.GsonUtils;
import com.blankj.utilcode.util.ObjectUtils;
import com.blankj.utilcode.util.SizeUtils;
import com.blankj.utilcode.util.StringUtils;
import com.eostoken.eos_wallet.R;
import com.eostoken.eos_wallet.base.BaseFragment;
import com.eostoken.eos_wallet.bean.AccountInfo;
import com.fredy.mvp.BasePresenter;
import com.fredy.mvp.BaseView;
import com.fredy.mvp.Model;
import com.qmuiteam.qmui.widget.QMUITopBarLayout;

import java.math.BigDecimal;
import java.util.List;

import butterknife.BindView;

import static com.eostoken.eos_wallet.utils.Constants.EXTRA_ACCOUNT_INFO;

public class PermissionsViewFragment extends BaseFragment {
    @BindView(R.id.topbar)
    QMUITopBarLayout topbar;
    @BindView(R.id.activeTv)
    AppCompatTextView activeTv;
    @BindView(R.id.activeWeightValueTv)
    AppCompatTextView activeWeightValueTv;
    @BindView(R.id.publickeyTv1)
    AppCompatTextView publickeyTv1;
    @BindView(R.id.weightTv1)
    AppCompatTextView weightTv1;
    @BindView(R.id.ownerTv)
    AppCompatTextView ownerTv;
    @BindView(R.id.ownerWeightValueTv)
    AppCompatTextView ownerWeightValueTv;
    @BindView(R.id.publickeyTv2)
    AppCompatTextView publickeyTv2;
    @BindView(R.id.weightTv2)
    AppCompatTextView weightTv2;
    @BindView(R.id.ownerExplainTv)
    AppCompatTextView ownerExplainTv;
    @BindView(R.id.activeExplainTv)
    AppCompatTextView activeExplainTv;
    @BindView(R.id.weightExplainTv)
    AppCompatTextView weightExplainTv;
    @BindView(R.id.iconExplainTv)
    AppCompatTextView iconExplainTv;
    @BindView(R.id.drawcirleIv1)
    AppCompatImageView drawcirleIv1;
    @BindView(R.id.drawcirleIv2)
    AppCompatImageView drawcirleIv2;

    private String accountInfoJSON;
    private AccountInfo accountInfo;

    @Override
    protected int getContentLayout() {
        return R.layout.fragment_permissions_view;
    }

    @Override
    protected void initData() {
        Bundle bundle = getArguments();
        accountInfoJSON = bundle.getString(EXTRA_ACCOUNT_INFO, "");
        if (!ObjectUtils.isEmpty(accountInfoJSON)) {
            accountInfo = GsonUtils.fromJson(accountInfoJSON, AccountInfo.class);
        }
    }

    @Override
    protected void initView(View contentView) {
        topbar.setTitle(StringUtils.getString(R.string.str_permissions_view));
        topbar.addLeftBackImageButton().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popOrFinish();
            }
        });
        initView();
    }

    private void initView() {
        if (!ObjectUtils.isEmpty(accountInfo)) {
            List<AccountInfo.PermissionsBean> permissionsBeanList = accountInfo.getPermissions();
            AccountInfo.PermissionsBean activePermissionsBean = permissionsBeanList.get(0);
            AccountInfo.PermissionsBean ownerPermissionsBean = permissionsBeanList.get(1);

            String activeName = activePermissionsBean.getPerm_name();
            String ownerName = ownerPermissionsBean.getPerm_name();

            AccountInfo.PermissionsBean.RequiredAuthBean activeRequiredAuthBean = activePermissionsBean.getRequired_auth();
            AccountInfo.PermissionsBean.RequiredAuthBean ownerRequiredAuthBean = ownerPermissionsBean.getRequired_auth();

            BigDecimal activeThreshold = activeRequiredAuthBean.getThreshold();
            BigDecimal ownerThreshold = ownerRequiredAuthBean.getThreshold();

            List<AccountInfo.PermissionsBean.RequiredAuthBean.KeysBean> activeKeysBeanList = activeRequiredAuthBean.getKeys();
            List<AccountInfo.PermissionsBean.RequiredAuthBean.KeysBean> ownerKeysBeanList = ownerRequiredAuthBean.getKeys();

            AccountInfo.PermissionsBean.RequiredAuthBean.KeysBean activeKeysBean = activeKeysBeanList.get(0);
            AccountInfo.PermissionsBean.RequiredAuthBean.KeysBean ownerKeyBean = ownerKeysBeanList.get(0);

            String activeKey = activeKeysBean.getKey();
            BigDecimal activeWeight = activeKeysBean.getWeight();

            String ownerKey = ownerKeyBean.getKey();
            BigDecimal ownerWeight = ownerKeyBean.getWeight();

            String activethreshold = activeThreshold.stripTrailingZeros().toPlainString();
            String ownerthreshold = ownerThreshold.stripTrailingZeros().toPlainString();
            if (ObjectUtils.isEmpty(activethreshold)) {
                activethreshold = "0";
            }
            if (ObjectUtils.isEmpty(ownerthreshold)) {
                ownerthreshold = "0";
            }
            activeTv.setText(activeName);
            activeWeightValueTv.setText(String.format(StringUtils.getString(R.string.str_weight_value), activethreshold));
            publickeyTv1.setText(activeKey);
            weightTv1.setText(activeWeight.stripTrailingZeros().toPlainString());
            ownerTv.setText(ownerName);
            ownerWeightValueTv.setText(String.format(StringUtils.getString(R.string.str_weight_value), ownerthreshold));
            publickeyTv2.setText(ownerKey);
            weightTv2.setText(ownerWeight.stripTrailingZeros().toPlainString());

        }
    }

    @Override
    public Model createModel() {
        return null;
    }

    @Override
    public BaseView createView() {
        return null;
    }

    @Override
    public BasePresenter createPresenter() {
        return null;
    }

    @Override
    public void finishRefresh() {

    }
}
